package work20240808.work6;

public class FizzBuzz {
    private int n;
    private int current = 1;

    public FizzBuzz(int n) {
        this.n = n;
    }

    public int getN() {
        return n;
    }

    public void setN(int n) {
        this.n = n;
    }


    public int getCurrent() {
        return current;
    }

    public void setCurrent(int current) {
        this.current = current;
    }

    public synchronized void fizz(Runnable printFizz) {
        while (current <= n) {
            if (current % 3 == 0 && current % 5 != 0) {
                printFizz.run();
                current++;
                notifyAll();
            } else {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        }
    }          // only output "fizz"
    public synchronized void buzz(Runnable printBuzz) {
        while (current <= n) {
            if (current % 5 == 0 && current % 3 != 0) {
                printBuzz.run();
                current++;
                notifyAll();
            } else {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        }
    }          // only output "buzz"
    public synchronized void fizzbuzz(Runnable printFizzBuzz) {
        while (current <= n) {
            if (current % 5 == 0 && current % 3 == 0) {
                printFizzBuzz.run();
                current++;
                notifyAll();
            } else {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        }
    }  // only output "fizzbuzz"
    public synchronized void number(Runnable printNumber) {
        while (current <= n) {
            if (current % 5 != 0 && current % 3 != 0) {
                printNumber.run();
                current++;
                notifyAll();
            } else {
                try {
                    wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }

            }
        }
    }
}
